#[derive(Debug, PartialEq)]
pub struct Circle {
    pub x: f64,
    pub y: f64,
    pub radius: f64,
}


//通过两点和半径创建一个圆
pub fn from_points_and_radius(point1: &Point, point2: &Point, radius: f64) -> Option<Circle> {
    let center_x = (point1.x + point2.x) / 2.0;
    let center_y = (point1.y + point2.y) / 2.0;

    let distance =((point2.x-point1.x).powi(2)+(point2.y-point1.y).powi(2)).sqrt();

    info!("圆的半径为 {}，圆心为（{},{}）",distance/2,center_x,center_y);

    if radius == distance/2.0 {
        Some(Circle {
            x: center_x,
            y: center_y,
            radius,
        })
    } else {
        None
    }
}

//通过三点创建一个圆
pub fn form_threes_points(p1: &Point, p2: &Point, p3: &Point) -> Option<Circle>{
    //计算圆心坐标
    let h=(p1.x+p2.x)/2.0;
    let k=(p1.y+p2.y)/2.0;

    //计算半径
    let r =((p2.x-p1.x).powi(2)+(p2.y-p1.y).powi(2)).sqrt()/2.0;

    //验证第三个点是否在圆上
    //第三点到圆心的距离
    let threesP_to_c_distance = ((p3.x-h).powi(2)+(p3.y-k).powi(2)).sqrt();
    if threesP_to_c_distance == r{
        Some(Circle{
            x:h,
            y:k,
            radius:r,
        })
    }else{
        None
    }
}

//圆上的点
pub fn gennerate_points_on_circle(center_x: f64,center_x: f64, radius: f64, num_points:usize) ->Vec<Point> {
    let mut points = Vec::with_capacity(num_points);

    //计算每个点的角度 步长
    let angle_step = 2.0 * PI / num_points as f64;

    //生成点的坐标
    for i in 0..num_points {
        let angle = i as f64 * angle_step;
        let x = center_x + radius * angle.cos();
        let y = center_x + radius * angle.sin();
        points.push(Point { x, y });
    }

    points

}

//圆的关系

/// 判断两个圆是否相交
pub fn circles_intersect(circle1: &Circle, circle2: &Circle) -> bool {
    //计算两个圆心的距离
    let distance = ((circle1.x-circle2.x).powi(2)+(circle1.y-circle2.y).powi(2)).sqrt();
    //两个圆心的距禿小于两个圆的半径之和，则相交
    distance<(circle1.radius+circle2.radius)
}

/// 判断两个圆是否相切
pub fn circles_tangent(circle1: &Circle, circle2: &Circle) -> bool {
    //计算两个圆心的距离
    let distance = ((circle1.x-circle2.x).powi(2)+(circle1.y-circle2.y).powi(2)).sqrt();
    //两个圆心的距禿等于两个圆的半径之和，则相切
    distance==(circle1.radius+circle2.radius)
}


/// 判断是否包含一个圆
pub fn circle_contains(&self, other: &Circle) -> bool {
    //计算两个圆心的距离
    let distance = ((self.x-other.x).powi(2)+(self.y-other.y).powi(2)).sqrt();
    //一个圆的半径大于另一个圆心的距离加上另一个圆的半径，则包含
    self.radius>distance+other.radius
}